#include <jni.h>
#include <string>
#include "XLog.h"
#include <android/native_window_jni.h>
#include "IPlayerProxy.h"

extern "C"
JNIEXPORT
jint JNI_OnLoad(JavaVM *vm,void *res)
{
    IPlayerProxy::Get()->Init(vm);
//    IPlayerProxy::Get()->Open("rtmp://192.168.1.9:1935/live/livestream");
//    IPlayerProxy::Get()->Open("http://ivi.bupt.edu.cn/hls/cctv1.m3u8");
//    IPlayerProxy::Get()->Start();
//    IPlayerProxy::Get()->Open("/sdcard/1080.mp4");
//    IPlayerProxy::Get()->Start();
//    IPlayerProxy::Get()->Open("/sdcard/1080p-2.mkv");
//    IPlayerProxy::Get()->Start();

    return JNI_VERSION_1_4;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_pengtgimust_androidvideoplayer_XPlay_InitView(JNIEnv *env, jobject thiz, jobject surface) {
    // TODO: implement InitView()
    ANativeWindow * win = ANativeWindow_fromSurface(env,surface);
    IPlayerProxy::Get()->InitView(win);
}

extern "C"
JNIEXPORT jdouble JNICALL
Java_com_pengtgimust_androidvideoplayer_MainActivity_PlayPos(JNIEnv *env, jobject thiz) {
    // TODO: implement PlayPos()
    return IPlayerProxy::Get()->PlayPos();
}

extern "C"
JNIEXPORT void JNICALL
Java_com_pengtgimust_androidvideoplayer_MainActivity_Seek(JNIEnv *env, jobject thiz, jdouble pos_) {
    // TODO: implement Seek()

    IPlayerProxy::Get()->Seek(pos_);
}

extern "C"
JNIEXPORT void JNICALL
Java_com_pengtgimust_androidvideoplayer_OpenUrl_Open(JNIEnv *env, jobject thiz, jstring url_) {
    // TODO: implement Open()
    const char *url = env->GetStringUTFChars(url_,0);
    IPlayerProxy::Get()->Open(url);
    IPlayerProxy::Get()->Start();
//    IPlayerProxy::Get()->Seek(0.5);
}extern "C"
JNIEXPORT void JNICALL
Java_com_pengtgimust_androidvideoplayer_XPlay_PlayOrPause(JNIEnv *env, jobject thiz) {
    // TODO: implement PlayOrPause()
    IPlayerProxy::Get()->SetPause(!IPlayerProxy::Get()->IsPause());
}